using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
using System.Linq;
using LitJson;
namespace NodeEditor
{
    public abstract class AbstractNode : ScriptableObject
    {
        [Input] [HideInInspector] public Connect input;
        [Output] [HideInInspector] public Connect output;

        [LitJson.Extensions.JsonIgnore] [HideInInspector] public Vector2 position;
        [HideInInspector] public string guid;
        [HideInInspector] public virtual string title => string.Empty;
        [HideInInspector] public List<Port> ports = new List<Port>();

        public AbstractNode() { }
        public ScriptableObject Clone()
        {
            
            return null;
        }

        public override string ToString()
        {
            return $"{guid}>{title}";
        }

#if UNITY_EDITOR
        [HideInInspector] public string explain;
        static bool isDebug = true;
        public void Save()
        {
            UnityEditor.EditorUtility.SetDirty(this);
        }
        public VisualElement[] DrawGUI()
        {
            List<VisualElement> visualElements = new List<VisualElement>();

            OnDrawGUI(visualElements);


            TextField textField = new TextField() { multiline = true };
            textField.value = this.explain;
            TextFieldBinding.Binding(textField, text =>
            {
                this.explain = text;
                this.Save();
            });

            visualElements.Add(textField);

            if(isDebug)
            {
                Label label = new Label($"guid: {guid}");
                visualElements.Add(label);

                var array = ports.Where(p => p.direction == PortDirection.Input).ToArray();
                foreach (var port in array)
                {
                    label = new Label($"input: {port.guid}");
                    visualElements.Add(label);
                }
                array = ports.Where(p => p.direction == PortDirection.Output).ToArray();
                foreach (var port in array)
                {
                    label = new Label($"ouput: {port.guid}");
                    visualElements.Add(label);
                }
            }

            return visualElements.ToArray();
        }

        protected virtual void OnDrawGUI(List<VisualElement> visualElements)
        {

        }
#endif



    }

    

    [System.Serializable]
    public class Connect 
    {
    
    }
}